package pool

import (
	"github.com/gomodule/redigo/redis"
	"log"
	"time"
)

var (
	pool *redis.Pool
)

const (
	redis_passwd = ""
	redis_host   = ""
)

/**
初始化redis连接池
*/
func Init() {
	if len(redis_passwd) <= 0 || len(redis_host) <= 0 {
		log.Fatal("redis 缺少基础配置！")
	}
	pool = &redis.Pool{
		MaxIdle:     10,
		MaxActive:   0, //0表示没有限制
		IdleTimeout: 1 * time.Second,
		Dial: func() (redis.Conn, error) {
			return redis.Dial("tcp", redis_host, redis.DialPassword(redis_passwd))
		},
	}
}

func GetRedisConn() redis.Conn {
	conn := pool.Get()
	if conn.Err() != nil {
		log.Fatal("redis connect fail!", conn.Err())
	}
	return conn
	//conn, err := redis.Dial("tcp", redis_host, redis.DialPassword(redis_passwd))
	//if err != nil {
	//	log.Println("redis connect err", err)
	//	panic(err)
	//}
	//return conn
}
